﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            var sql = "select * from Users";
            var sqlArt = "select * from Articles";
            var dt = DbHelper.GetDataTable(sqlArt);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //查找//
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Tible like '%{0}%'",name);
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }
         
        private void button2_Click(object sender, EventArgs e)
        {
            //添加
            Form4 form4 = new Form4();
            var res = form4.ShowDialog();
            if (res==DialogResult.Yes)
            {
                
                var name = textBox1.Text;
                var sqlArt = string.Format("select * from Articles where Tible like '%{0}%'", name);
                var dt = DbHelper.GetDataTable(sqlArt);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("取消添加");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //更新
            if (dataGridView1.SelectedRows.Count>0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;
                var tible = (string)dataGridView1.SelectedRows[0].Cells["Tible"].Value;
                var author =(string) dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdtime = (string )dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;


                Form4 form4 = new Form4(id, tible, author, createdtime);
                var res=form4.ShowDialog();

                if (res==DialogResult.Yes)
                {
                    var nameTemp = textBox1.Text;
                    var sqlArt = string.Format("select * from Articles where Tible like '%{0}%'", nameTemp);
                    var dt = DbHelper.GetDataTable(sqlArt);

                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("这个是no");
                }
            }
            else
            {
                MessageBox.Show("您未选择任何的数据", "提示");
            }
           




           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
            var row = dataGridView1.SelectedRows[0];
            var id = (int)row.Cells["id"].Value;
                var res = MessageBox.Show("确定要删除嘛", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where id={0} ", id);
                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;
                        var sqlArt =string.Format( "select * from Articles where Tible like '%{0}%'",name);
                        var dt = DbHelper.GetDataTable(sqlArt);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("没有任何选项", "提示");
                    }



                }
                
            }
         
        
            //删除
        }
    }
}
